package com.example.mycarcalling.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.example.lib_cache.model.User;
import com.example.lib_cache.viewmodel.IndexActivityViewModel;
import com.example.lib_common.util.LiveDataManager;
import com.example.lib_common.util.SpUtils;
import com.example.mycarcalling.R;
import com.example.mycarcalling.databinding.ActivityIndexBinding;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class IndexActivity extends AppCompatActivity {

    private final static String TAG = IndexActivity.class.getSimpleName();

    private final static Integer TO_LOGIN = 0;
    private final static Integer LOGIN_FINISH = 1;

    private MutableLiveData<String> liveData;
    private LiveData<User> userLiveData;
    private ActivityIndexBinding activityIndexBinding;
    private IndexActivityViewModel indexActivityViewModel;
    private Fragment fragmentId;
    private NavController navController;

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityIndexBinding =  DataBindingUtil.setContentView(
                this,R.layout.activity_index);
//        setContentView(R.layout.activity_index);
        AndPermission.with(this)
                .runtime()
                .permission(Permission.RECORD_AUDIO,Permission.READ_EXTERNAL_STORAGE,Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.ACCESS_COARSE_LOCATION,Permission.ACCESS_FINE_LOCATION)
                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> permissions) {
                        Log.d(TAG, "onAction(List<String>) called in onGranted, permissions: " + permissions);
                    }
                })
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        Log.d(TAG, "onAction(List<String>) called in onDenied, data: " + data);
                    }
                }).start();

        context = this;
        initView();

        //创建数据源
        //创建一个ViewModel的工厂
        ViewModelProvider.Factory factory = ViewModelProvider.
                AndroidViewModelFactory.getInstance(getApplication());
        //创建ViewModel的管理对象
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        //生产ViewModel
        indexActivityViewModel = viewModelProvider.get(IndexActivityViewModel.class);
        userLiveData = LiveDataManager.getInstance().with("user",User.class);

        initEvent();
        Log.d("IndexActivity","开始获取token");
        String cctoken  = SpUtils.getString(context,"cctoken");
        Log.d(TAG,cctoken);
        if(cctoken.equals("")){
            Log.d(TAG,"token数据未缓存");
        }else {
            liveData =  LiveDataManager
                    .getInstance().with("cctoken",String.class);
            liveData.setValue(cctoken);
        }






    }

    private void initView(){
        fragmentId = getSupportFragmentManager()
                .findFragmentById(R.id.nav_host);
        navController = NavHostFragment.findNavController(fragmentId);
        //将navControllery与navBottom绑定
        NavigationUI.setupWithNavController(activityIndexBinding.navBottom,navController);
        setTitle("主页");
    }

    private void initEvent(){
        userLiveData.observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                Log.d("当前登录：",user.getAccount());
            }
        });
//        Log.d("登录用户：",userLiveData.getValue().getAccount());

        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
//                    Log.d(TAG,"切换的目标:"+destination.getLabel());
//                    Log.d(TAG,"切换的目标:"+destination.getNavigatorName());
                switch (destination.getLabel().toString()){
                    case "userfragment":
                        setTitle("个人中心");
                        break;
                    case "mapfragment":
                        setTitle("主页");
                        break;

                }


            }
        });
    }

    private void intentForResult(Intent intent,int request){
        startActivityForResult(intent,request);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(LOGIN_FINISH == resultCode){

        }
    }
}
